package com.seeyon.apps.nygj.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.filemanager.manager.FileManager;
import com.seeyon.ctp.common.po.filemanager.V3XFile;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileKit {
    private static FileManager fileManager = (FileManager) AppContext.getBean("fileManager");

    public static void uploadFile(List<Long> list, String guid, Long memberId, String formCode, String ip) throws Exception {
        /*获取token*/
        String base64Str = SystemKit.RES_APP + ":" + SystemKit.key;
        String Authorization = StrKit.stringToBase64(base64Str);
        Map<String, Object> headerMap = new HashMap<String, Object>();
        headerMap.put("appID", SystemKit.RES_APP);
        headerMap.put("Authorization", "Basic " + Authorization);
        headerMap.put("appIDnew", SystemKit.appIDnew);
        headerMap.put("keySecretnew", SystemKit.keySecretnew);
        String result = HttpKit.get(SystemKit.tokenUrl, headerMap);

        JSONObject tokenObj = JSONObject.parseObject(result);
        String code_token = StrKit.str(tokenObj.get("code"));

        if ("00000".equals(code_token)) {
            String token = StrKit.str(tokenObj.getJSONObject("data").get("token"));
            Map<String, Object> dataHeaderMap = new HashMap<String, Object>();
            dataHeaderMap.put("appID", SystemKit.RES_APP);
            dataHeaderMap.put("Authorization", "Basic " + Authorization);
            dataHeaderMap.put("auth-token", token);
            Map<String, Object> bodyMap = new HashMap<String, Object>();
            bodyMap.put("GUID", guid);
            bodyMap.put("MDLID", "MDHTDJ");
            String tokenUrl = ip + "/seeyon/rest/token";
            Map<String, Object> tokenMap = new HashMap<String, Object>();
            Map<String, Object> headersMap = new HashMap<String, Object>();
            tokenMap.put("userName", "rest_jcpt");
            OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
            V3xOrgMember v3xOrgMember = orgManager.getMemberById(memberId);
            tokenMap.put("loginName", v3xOrgMember.getLoginName());
//            tokenMap.put("password", "c4d5f528-7184-4035-8be3-40b4462e02fc");
            /*正式环境*/
            tokenMap.put("password", "42343e43-66d0-4041-b2ec-dd7c81f8b100");
            String tokenJson = JSON.toJSONString(tokenMap);
            String token_result = HttpKit.post(tokenUrl, headersMap, tokenJson);
            String tokenId = "";
            try {
                JSONObject token_obj = JSONObject.parseObject(token_result);
                tokenId = StrKit.str(token_obj.get("id"));
            } catch (JSONException e) {
            }
            for (Long fileUrl : list) {
                V3XFile v3xFile = fileManager.getV3XFile(fileUrl);
                String fileName = v3xFile.getFilename();
                String fileNameNew = URLEncoder.encode(fileName, "UTF-8");
                String uploadFileUrl = ip+"/seeyon/rest/attachment/file/"+v3xFile.getId()+"?fileName="+fileNameNew+"&token="+tokenId;
                File file = HttpKit.getFile(uploadFileUrl, v3xFile.getFilename());
                String fileResult = HttpKit.postFile(SystemKit.fileUrl, dataHeaderMap, bodyMap, file);
                FormCap4Kit.addLogs(memberId, "附件上传接口", formCode, "JKCGBS_S", "", JSON.toJSONString(fileResult));
            }
        }
    }
}
